import VueRouter from 'vue-router'
import { enhance } from '@/utils'
import Home from '@/pages/Home'
import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Vue from 'vue'
import NotFound from '@/pages/NotFound'
import Study from '@/pages/Study'
import Detail from '@/pages/Detail'
import AddCart from '@/pages/AddCart'
// 2- 安装
Vue.use(VueRouter)
VueRouter.prototype.goSearch = function (query) {
  this.push({
    path: '/search',
    query: {
      ...this.history.current.query,
      pageNo: 1,
      ...query,
    },
  })
}

// 3
enhance('push')
enhance('replace')

const routes = [
  {
    // 首页
    path: '/',
    component: Home,
    meta: {
      isTypeNav: true,
      isFooterList: true,
    },
  },
  {
    // 搜索页
    path: '/search',
    component: Search,
    meta: {
      isTypeNav: true,
      isFooterList: true,
    },
  },
  {
    path: '/login',
    name: 'lala',
    component: Login,
  },
  {
    path: '/register',
    component: Register,
  },
  {
    // 如果访问的地址为/home,那么会被重写向至/
    path: '/home',
    redirect: '/',
  },
  {
    // 当找不到匹配的路由时生效。
    path: '*',
    component: NotFound,
  },
  {
    path: '/study',
    component: Study,
  },
  {
    path: '/detail/:id',
    component: Detail,
    meta: {
      isScroll: true, // 希望滚动条横向以及纵向的值为0
      isTypeNav: true,
      isFooterList: true,
    },
  },
  {
    path: '/addCart',
    name: 'addCart',
    component: AddCart,
    meta: {
      isScroll: true, // 希望滚动条横向以及纵向的值为0
      isFooterList: true,
    },
  },
]

export default new VueRouter({
  // 路由器
  routes,
  // 路由的模式
  mode: 'history',
  // 1- 接收的参数是什么：to,from
  // 2- 返回的是什么：对象
  // 3- 什么时候执行?答：当我的路由信息发生改变即会被调用。
  scrollBehavior(to, from) {
    // console.log('to', to)
    // console.log('from', from)
    if (to.meta.isScroll) {
      return {
        x: 0, // 横向滚动条
        y: 0, // 纵向滚动条
      }
    }
  },
})
